package com.chinaoct.siku.gzw.controller;

import com.chinaoct.siku.LocalStorage.config.FileProperties;
import com.chinaoct.siku.LocalStorage.dao.StorageDTO;
import com.chinaoct.siku.LocalStorage.dao.StorageVO;
import com.chinaoct.siku.LocalStorage.service.StorageService;
import com.chinaoct.siku.gzw.dao.ShangbaoDTO;
import com.chinaoct.siku.gzw.services.ShangbaoService;
import com.chinaoct.siku.param.BlockParam;
import com.chinaoct.siku.util.ResponseMsg;
import com.chinaoct.siku.util.UserContext;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping(value = "/api/shangbao")
public class ShangbaoController {

    @Autowired
    private ShangbaoService shangbaoService;
    @Autowired
    private StorageService storageService;

    @ApiOperation(value = "分页查询模块")
    @PostMapping(value = "/page")
    public ResponseMsg pageMeeting(@RequestBody @Valid BlockParam param){
        Sort sort=new Sort(Sort.Direction.DESC,"createTIme");
        Pageable pageable= PageRequest.of(param.getPage(),param.getPageSize(),sort);
        Page<ShangbaoDTO> pages=shangbaoService.pageShangbao(pageable);
        return new ResponseMsg(pages);
    }

    @ApiOperation(value = "上传文件")
    @PostMapping("addFile")
    public ResponseMsg addFile(@RequestParam("file") MultipartFile file,
                               @RequestParam("dataDate") String dataDate,
                               @RequestParam("enableFile") Integer enableFile) {
        try {
            StorageDTO storageDTO = storageService.saveFile(file.getName(), FileProperties.GZW_SOURCE, file);
            boolean b=shangbaoService.uploadFile(storageDTO,dataDate,enableFile);
            storageService.deleteById(storageDTO.getId());
            if (b) {
                return new ResponseMsg();
            }else {
                return ResponseMsg.error("-1","文件上传失败");
            }
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return ResponseMsg.error("-1",e.getMessage());
        }

    }

}
